<template>
  <div>
    <el-card>
      <el-row style="margin-bottom: 10px">
        <el-col span="auto" style="margin-right: 10px">
          <el-input v-model="searchKeyWord" placeholder="输入关键字搜索"></el-input>
        </el-col>
        <el-col :span="2" style="margin-right: 10px">
          <el-select v-model="selectPostType" placeholder="帖子类型" value="">
            <el-option label="" value=""></el-option>
            <el-option label="认领" value="1"></el-option>
            <el-option label="寻物" value="0"></el-option>
          </el-select>
        </el-col>
        <el-col :span="4" style="margin-right: 10px">
          <el-select v-model="selectItemType" value="" placeholder="物品类型">
            <el-option label="" value=""></el-option>
            <el-option v-for="item in itemTypeList" :label="item.type" :value="item.id"></el-option>
          </el-select>
        </el-col>
        <el-col :span="2" style="margin-right: 10px">
          <el-select v-model="selectReivewStatus" placeholder="审核状态" value="">
            <el-option label="" value=""></el-option>
            <el-option label="未审核" value="0"></el-option>
            <el-option label="已通过" value="2"></el-option>
            <el-option label="未通过" value="1"></el-option>
          </el-select>
        </el-col>
        <el-col :span="2" style="margin-right: 10px">
          <el-select v-model="selectCompleteStatus" placeholder="完成状态" value="">
            <el-option label="" value=""></el-option>
            <el-option label="已完成" value="1"></el-option>
            <el-option label="未完成" value="0"></el-option>
          </el-select>
        </el-col>
        <el-col span="auto" style="margin-right: 10px">
          <el-button type="primary" icon="el-icon-search" @click="search">搜索</el-button>
        </el-col>
      </el-row>
      <el-table :data="tableData" border>
        <el-table-column label="ID" prop="id" width="60" header-align="center" align="center" sortable>
        </el-table-column>
        <el-table-column label="帖子类型" prop="type" width="80" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag effect="dark" :type="scope.row.type ? 'primary' : 'success'">{{
              postType[scope.row.type] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="物品类型" prop="itemTypeName" width="130" header-align="center" align="center">
          <template slot-scope="scope">
            <span>{{ scope.row.itemTypeName }}</span>
          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title" width="200" header-align="center" align="center">
          <template slot-scope="scope">
            <span v-html="scope.row.title"></span>
          </template>
        </el-table-column>
        <el-table-column label="审核状态" prop="reviewStatus" width="100" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag :type="postReviewType[scope.row.reviewStatus]">{{
              postReviewStatus[scope.row.reviewStatus] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="完成状态" prop="completeStatus" width="100" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag :type="scope.row.completeStatus ? 'success' : 'info'" disable-transitions>{{
              postCompleteStatus[scope.row.completeStatus] }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="注册时间" prop="gmtCreate" width="170" header-align="center" align="center" sortable>
        </el-table-column>
        <el-table-column label="修改时间" prop="gmtModified" width="170" header-align="center" align="center"
          sortable></el-table-column>
        <el-table-column label="操作" header-align="center" align="center">
          <template slot-scope="scope">
            <el-button size="mini" type="primary" @click="handleOpen(scope.row)" style="margin-right: 10px">查看</el-button>
            <el-button size="mini" @click="deleteDialogOpen(scope.row)" type="danger">删除</el-button>
            <el-dialog title="确认删除" :visible.sync="deleteDialog" width="30%">
              <span slot="footer" class="dialog-footer">
                <el-button @click="deleteDialog = false">取 消</el-button>
                <el-button type="primary" @click="deletePost">确 定</el-button>
              </span>
            </el-dialog>
          </template>
        </el-table-column>
      </el-table>
      <el-dialog title="帖子详情" :visible.sync="openDialog" width="35%">
        <el-form>
          <PostDetail :postId="form.id" :key="form.id"></PostDetail>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="openDialog = false">确 定</el-button>
        </span>
      </el-dialog>
      <!-- 底部分页 -->
      <div style="text-align: center">
        <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page.sync="currentPage" :page-sizes="[10, 15, 20]" :page-size="pageSize" :total="totalSize" background
          layout="total, sizes, prev, pager, next, jumper">
        </el-pagination>
      </div>
    </el-card>
  </div>
</template>

<script>
import { searchByMultipleCondition } from '@/api/search/post'
import { getItemTypeListAll } from "@/api/post/itemType"
import { deletePostById } from "@/api/post/post"
import PostDetail from '../not-reviewed/components/PostDetail.vue'

export default {
  components: {
    PostDetail,
    // PostDetail
  },
  provide() {
    return {
      getDataList: this.getDataList,
    }
  },
  data() {
    return {
      dataList: [],
      tableData: [],
      searchKeyWord: '',
      currentPage: 1,
      pageSize: 10,
      totalSize: 0,
      postType: ['寻物', '认领'],
      postReviewType: ['info', 'danger', 'scucess'],
      postReviewStatus: ['未审核', '未通过', '已通过'],
      postCompleteStatus: ['未完成', '已完成'],
      itemTypeList: [],
      dynamicTags: [],
      openDialog: false,
      deleteDialog: false,
      disableTimeType: 0,
      selectPostType: '',
      selectItemType: '',
      selectReivewStatus: '',
      selectCompleteStatus: '',
      form: {}
    }
  },
  methods: {
    init(data) {
      this.form.id = data.id
    },
    handleSizeChange(val) {
      this.pageSize = val
      this.getDataList()
    },
    handleCurrentChange(val) {
      this.currentPage = val
      this.getDataList()
    },
    deleteDialogOpen(row) {
      this.deleteDialog = true
      this.init(row)
    },
    handleOpen(row) {
      this.init(row)
      this.openDialog = true
    },
    deletePost() {
      deletePostById(this.form.id).then(res => {
        if (res.data.code === 200) {
          this.deleteDialog = false
          this.$message.success(res.data.message)
        } else {
          // this.$message.error(res.data.message)
        }
      })
    },
    getItemTypeListAll() {
      getItemTypeListAll().then(res => {
        if (res.data.code === 200) {
          this.itemTypeList = res.data.itemTypeInfoList
        } else {
          // this.$message.error(res.data.message)
        }
      })
    },
    getDataList() {
      searchByMultipleCondition({
        keyword: this.searchKeyWord === '' ? undefined : this.searchKeyWord,
        pageNo: this.currentPage,
        pageSize: this.pageSize,
        type: this.selectPostType === '' ? undefined : Number(this.selectPostType),
        itemTypeId: this.selectItemType === 0 ? undefined : this.selectItemType,
        reviewStatus: this.selectReivewStatus === '' ? undefined : Number(this.selectReivewStatus),
        completeStatus: this.selectCompleteStatus === '' ? undefined : Number(this.selectCompleteStatus),
      }).then(res => {
        if (res.data.code === 200) {
          this.dataList = res.data.searchResult.dataList
          this.tableData = this.dataList
          this.totalSize = res.data.searchResult.totalCount
        } else {
          // this.$message.error({
          //   message: res.data.message,
          //   duration: 1000
          // })
        }
      })
    },
    search() {
      searchByMultipleCondition({
        keyword: this.searchKeyWord === '' ? undefined : this.searchKeyWord,
        pageNo: 1,
        pageSize: this.pageSize,
        type: this.selectPostType === '' ? undefined : Number(this.selectPostType),
        itemTypeId: this.selectItemType === 0 ? undefined : this.selectItemType,
        reviewStatus: this.selectReivewStatus === '' ? undefined : Number(this.selectReivewStatus),
        completeStatus: this.selectCompleteStatus === '' ? undefined : Number(this.selectCompleteStatus),
      }).then(res => {
        if (res.data.code === 200) {
          this.tableData = res.data.searchResult.dataList
          this.totalSize = res.data.searchResult.totalCount
        } else {
          // this.$message.error({
          //   message: res.data.message,
          //   duration: 1000
          // })
        }
      })
    },
  },
  created() {
    this.getDataList()
    this.getItemTypeListAll()
  }
}
</script>

<style lang="scss" scoped>
.el-pagination {
  margin-top: 20px;
}
</style>
